class Chat extends egret.Sprite {
	private roseHead: egret.Bitmap;
	private paulHead: egret.Bitmap;
	private whiteWord: egret.Bitmap;
	private greenWord: egret.Bitmap;

	

	public whole: egret.Sprite;

	private lastY = 136;
	private pic:egret.Bitmap;
	private pic1: egret.Bitmap;
	private pic2: egret.Bitmap;
	private pic3: egret.Bitmap;
	private picBlack: egret.Shape;


	private bl_touch;

	private picX;
	private picY;

	private bottom;

	private bl_num;
	private numMinus=54;

	private picNo=0;
	private arr_picXY;

	private bl_end0=false;
	public bl_end1=false;
	public bl_end2=false;
	private picnum=0;

	private arr_pic;


	private arr_tu;


	private bl_canRoll=false;

	private YY;
	private SYY;
	private bigSP:egret.Shape;
	private targetNum;



	public constructor() {
		super();
		this.roseHead = new egret.Bitmap(RES.getRes("RoseHead_png"));
		this.paulHead = new egret.Bitmap(RES.getRes("PaulHead_png"));
		this.whiteWord = new egret.Bitmap(RES.getRes("chatWhite_png"));
		this.greenWord = new egret.Bitmap(RES.getRes("chatGreen_png"));
		this.pic1 = new egret.Bitmap(RES.getRes("pic1_png"));
		this.pic2 = new egret.Bitmap(RES.getRes("pic2_png"));
		this.pic3 = new egret.Bitmap(RES.getRes("pic3_png"));

		this.arr_pic=new Array();
		for(var z=0;z<3;z++){
			var pic=new egret.Bitmap(RES.getRes("handpic_png"));
			pic.anchorOffsetX=pic.width/2;
			pic.anchorOffsetY=pic.height/2;
			egret.Tween.get(pic,{loop:true}).to({scaleX:0.5,scaleY:0.5},700).to({scaleX:1,scaleY:1},700);
			pic.alpha=0;
			this.arr_pic.push(pic);
		}

		this.pic2.anchorOffsetX = this.pic2.width / 2;
		this.pic2.anchorOffsetY = this.pic2.height / 2;
		this.whole = new egret.Sprite();
		this.addChild(this.whole);
		


		this.bigSP=new egret.Shape();
		this.bigSP.graphics.beginFill(0xff0000,0);
		this.bigSP.graphics.drawRect(0,0,640,100);
		this.whole.addChild(this.bigSP);



		this.picBlack = new egret.Shape();
		this.picBlack.graphics.beginFill(0x000000, 1);
		this.picBlack.graphics.drawRect(0, 0, 640, 1136);
		this.picBlack.alpha = 0;
		this.picBlack.x=this.picBlack.width/2;
		this.picBlack.y=this.picBlack.height/2;
		this.picBlack.anchorOffsetX=this.picBlack.width/2;
		this.picBlack.anchorOffsetY=this.picBlack.height/2;
		
		this.bl_touch = false;
		this.bottom=1010;
		this.bl_num=false;

		this.arr_picXY=new Array();
		this.arr_tu=new Array();

		GoEvent.msg.addEventListener("noClickPic",this.NoClickPic,this);
		
	}
	private NoClickPic(e:GoEvent){
		this.arr_tu[e.data-1].removeEventListener(egret.TouchEvent.TOUCH_BEGIN, this.TBPIC, this);
	}
	public SetBottom(num){
		egret.Tween.removeTweens(this.whole);
		this.bottom=num;
		var minus = this.whole.height - this.bottom;
		if (minus > 0) {
			egret.Tween.get(this.whole).to({ y: -minus }, 700, egret.Ease.cubicOut);
		}
		else if(minus<0&&this.whole.y<0){
			egret.Tween.get(this.whole).to({ y: 0 }, 700, egret.Ease.cubicOut);
		}
	}
	public DoIt(character, text, bl_pic) {

		var head;
		var word;
		var pic;
		
		if (character == "Rose") {
			head = new egret.Bitmap(this.roseHead.texture);
			word = new egret.Bitmap(this.whiteWord.texture);
			this.whole.addChild(head);
			this.whole.addChild(word);

			word=new NineSlices(text,character);
			this.whole.addChild(word);

			var balaY = this.lastY + 10;

			head.x = 16; head.y = balaY;
			word.x = 110; word.y = balaY;
		}
		else if (character == "Paul") {
			// console.log("Paul!!!",this.whole.height);
			head = new egret.Bitmap(this.paulHead.texture);
			word = new egret.Bitmap(this.greenWord.texture);
			word=new NineSlices(text,character);
			this.whole.addChild(head);
			this.whole.addChild(word);

			var balaY = this.lastY + 10;

			// console.log("PaulbalaY!!!",balaY);

			head.x = 541; head.y = balaY;
			word.x = 640-120-word.width; word.y = balaY;
		}

		if (bl_pic && character == "Rose") {
			this.picNo++;
			this.whole.removeChild(word);
			pic = new egret.Bitmap(RES.getRes("pic"+this.picNo+"_png"));
			pic.name=this.picNo;
			this.picBlack.scaleX=0;this.picBlack.scaleY=0;
			this.whole.addChild(this.picBlack);
			
			

			var balaY = this.lastY + 10;
			pic.scaleX=0.5;pic.scaleY=0.5;
			pic.x = 110 + pic.width / 2*0.5; pic.y = balaY + pic.height / 2*0.5;
			pic.anchorOffsetX = pic.width / 2; pic.anchorOffsetY = pic.height / 2;
			pic.touchEnabled=true;
			pic.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.TBPIC, this);
			this.whole.addChild(pic);

			this.arr_picXY.push(pic.x);this.arr_picXY.push(pic.y);
			this.picX=pic.x;
			this.picY=pic.y;
			// this.whole.addChild(this.arr_pic[this.picNo-1]);
			this.arr_pic[this.picNo-1].alpha=1;
			this.arr_pic[this.picNo-1].x=pic.x;
			this.arr_pic[this.picNo-1].y=pic.y;
			this.arr_tu.push(pic);
		}
		if(character=="Paul"&&bl_pic){
			this.picNo++;
			this.whole.removeChild(word);
			pic = new egret.Bitmap(RES.getRes("pic"+this.picNo+"_png"));
			pic.name=this.picNo;
			this.picBlack.scaleX=0;this.picBlack.scaleY=0;
			
			
			
			
			
			
			var balaY = this.lastY + 10;
			pic.scaleX=0.5;pic.scaleY=0.5;
			pic.x = 640-120-pic.width/4; pic.y = balaY +pic.height/4;
			pic.anchorOffsetX = pic.width / 2; pic.anchorOffsetY = pic.height / 2;
			pic.touchEnabled=true;
			pic.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.TBPIC, this);
			
			this.whole.addChild(pic);

			this.arr_picXY.push(pic.x);this.arr_picXY.push(pic.y);
			this.picX=pic.x;
			this.picY=pic.y;

			// this.whole.addChild(this.arr_pic[this.picNo-1]);
			this.arr_pic[this.picNo-1].alpha=1;
			this.arr_pic[this.picNo-1].x=pic.x;
			this.arr_pic[this.picNo-1].y=pic.y;

			this.arr_tu.push(pic);
		}


		if (bl_pic) {
			this.lastY = word.y + pic.height*0.5;
		}
		else {
			this.lastY = word.y + word.height;
		}
		this.SetBottom(1010-this.numMinus);
		
		this.bigSP.graphics.clear();
		this.bigSP.graphics.beginFill(0xff0000,0);
		this.bigSP.graphics.drawRect(0,0,640,this.whole.height);
		console.log("balaHeight:",this.bigSP.height,this.whole.height);
	
	}
	private BLACKCLICK(e:egret.TouchEvent){
		this.picBlack.touchEnabled=false;
		this.picBlack.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.BLACKCLICK,this);
		
		if(!this.bl_canRoll){
			this.arr_tu[this.targetNum-1].removeEventListener(egret.TouchEvent.TOUCH_BEGIN, this.TBPIC, this);
			}
			egret.Tween.get(this.picBlack).to({ alpha: 0}, 500, egret.Ease.cubicOut);
			
			egret.Tween.get(this.arr_tu[this.targetNum-1]).to({x:this.arr_picXY[(this.arr_tu[this.targetNum-1].name)*2-2], y: this.arr_picXY[2*this.arr_tu[this.targetNum-1].name-1], scaleX: 0.5, scaleY: 0.5 }, 500, egret.Ease.cubicOut);
			var This=this;
			var event=this.arr_tu[this.targetNum-1];
			setTimeout(function(){
				This.picBlack.scaleX=0;
				This.picBlack.scaleY=0;
				
				This.picnum++;
				if(event.name=="2"){
					This.bl_end1=true;
				}
				if(event.name=="3"){
					This.bl_end2=true;
				}
				if(This.bl_canRoll){
				This.addEventListener(egret.TouchEvent.TOUCH_BEGIN,This.FINALTB,This);	
				This.addEventListener(egret.TouchEvent.TOUCH_MOVE,This.FINALTM,This);
				}
				GoEvent.msg.dispatchEvent(new GoEvent("clickedPic","true"));
			},500)
			setTimeout(function(){
				if(This.targetNum==2){
				This.arr_tu[2].touchEnabled=true;
			}
			},500);
			this.bl_num=true;
			setTimeout(function(){
				This.bl_num=false;
			},500)
			
	}

	private TBPIC(e: egret.TouchEvent) {
		
		if(e.target.scaleX==1.1){	
			this.picBlack.touchEnabled=false;
			this.picBlack.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.BLACKCLICK,this);
			
			


			if(!this.bl_canRoll){
			e.target.removeEventListener(egret.TouchEvent.TOUCH_BEGIN, this.TBPIC, this);
			}
			egret.Tween.get(this.picBlack).to({ alpha: 0}, 500, egret.Ease.cubicOut);
			
			egret.Tween.get(e.target).to({x:this.arr_picXY[(e.target.name)*2-2], y: this.arr_picXY[2*e.target.name-1], scaleX: 0.5, scaleY: 0.5 }, 500, egret.Ease.cubicOut);
			var This=this;
			var event=e.target;
			setTimeout(function(){
				This.picBlack.scaleX=0;
				This.picBlack.scaleY=0;
				
				This.picnum++;
				if(event.name=="2"){
					This.bl_end1=true;
				}
				if(event.name=="3"){
					This.bl_end2=true;
				}
				if(This.bl_canRoll){
				This.addEventListener(egret.TouchEvent.TOUCH_BEGIN,This.FINALTB,This);	
				This.addEventListener(egret.TouchEvent.TOUCH_MOVE,This.FINALTM,This);
				}
				GoEvent.msg.dispatchEvent(new GoEvent("clickedPic","true"));



			},500)
			setTimeout(function(){
				if(This.targetNum==2){
				This.arr_tu[2].touchEnabled=true;
			}
			},500);
			this.bl_num=true;
			setTimeout(function(){
				This.bl_num=false;
			},500)
			
		}	
		else if(e.target.scaleX==0.5){
			GoEvent.msg.dispatchEvent(new GoEvent("clickedPic","false"));
			var This=this;

			setTimeout(function(){
				This.picBlack.touchEnabled=true;
				This.picBlack.addEventListener(egret.TouchEvent.TOUCH_BEGIN,This.BLACKCLICK,This);
			},800);
			
			this.whole.addChild(this.picBlack);
			this.whole.addChild(e.target);
			this.arr_pic[e.target.name-1].alpha=0;
			this.targetNum=e.target.name;
			e.target.alpha=1;
			egret.Tween.get(e.target).to({ x: 640 / 2, y: 1136 / 2-this.whole.y, scaleX: 1.1, scaleY: 1.1 }, 800, egret.Ease.cubicOut);
			this.picBlack.scaleX=1;this.picBlack.scaleY=1;
			this.picBlack.y= 1136 / 2-this.whole.y
			egret.Tween.get(this.picBlack).to({alpha:1},500,egret.Ease.cubicOut);
			this.bl_num=false;
			if(this.bl_canRoll){
			this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.FINALTB,this);	
			this.removeEventListener(egret.TouchEvent.TOUCH_MOVE,this.FINALTM,this);
			}
			if(this.targetNum==2){
				this.arr_tu[2].touchEnabled=false;
			}

		}


	}
	public NumWhetherTrue():boolean{
		return this.bl_num;
	}
	public LastPicShowEnd():boolean{
		if(this.bl_end1&&this.bl_end2){
			return true
		}
		else{
			return false;
		}
	}
	public SetCanRollTrue(){
		this.bl_canRoll=true;
		this.whole.touchEnabled=true;
		this.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.FINALTB,this);
		this.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.FINALTM,this);


		for(var z=0;z<this.arr_tu.length;z++){
			this.arr_tu[z].addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.TBPIC, this);
		}
	}
	private FINALTB(e:egret.TouchEvent){
		this.YY=e.stageY;
		this.SYY=this.whole.y;
	}
	private FINALTM(e:egret.TouchEvent){
		this.whole.y=e.stageY-this.YY+this.SYY;
		// if(this.whole.y+e.localY-this.YY<0){
		// 	this.whole.y+=e.localY-this.YY;
		// }
		if(this.whole.y>0){
			this.whole.y=0;
		}
		if(this.whole.y<-(this.whole.height-950)){
			this.whole.y=-(this.whole.height-950);
		}



	}

	
}